﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace VectorPicsEditor
{
    /// <summary>
    /// Interaction logic for CustomLine.xaml
    /// </summary>
    public partial class LineSelector : UserControl
    {
        public Line curLine;
        private Canvas canva;
        private bool first;

        public LineSelector(Line ln, Canvas canv, bool firstPoint)
        {
            InitializeComponent();
            first = firstPoint;
            curLine = ln;
            canva = canv;

            this.LinePositionChanged(null, null);
        }

        private void LinePositionChanged(object sender, EventArgs e)
        {
            if(first)
            {
                this.SetValue(Canvas.LeftProperty, (double)curLine.X1 - selector.Width / 2.0);
                this.SetValue(Canvas.TopProperty,  (double)curLine.Y1 - selector.Height / 2.0);
            }
            else
            {
                this.SetValue(Canvas.LeftProperty, (double)curLine.X2 - selector.Width / 2.0);
                this.SetValue(Canvas.TopProperty,  (double)curLine.Y2 - selector.Height / 2.0);
            }
        }

        private void SelectorCircle_MouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            Shape control = sender as Shape;
            control.CaptureMouse();
        }

        private void SelectorCircle_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton != MouseButtonState.Pressed )
                return;
            Point curMousePos = e.GetPosition(canva);
            if (first)
            {
                curLine.X1 = curMousePos.X;
                curLine.Y1 = curMousePos.Y;
            }
            else
            {
                curLine.X2 = curMousePos.X;
                curLine.Y2 = curMousePos.Y;
            }
            e.Handled = true;
            this.LinePositionChanged(null, null);
        }

        private void SelectorCircle_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Shape control = sender as Shape;
            control.ReleaseMouseCapture();
            e.Handled = true;
        }


    }
}
